<template>

  <section class="my_index">

    <Header fixed :title="title" showRight></Header>

    <div class="index_wrap pt-20">

      <div class="addbtn">

        <Button type="info" size="small" @click="getData">&nbsp;&nbsp;刷新&nbsp;&nbsp;</Button>

        <Button type="info" size="small" @click="$router.push(`/indexedit?action=${action}`)">&nbsp;&nbsp;添加&nbsp;&nbsp;</Button>

      </div>

      <!-- 标签1 -->

      <div class="my_box" v-if="action === 0">

        <div class="father_box" v-for="item in list" :key="item.id">

          <div class="fb_top">

            <div class="fb_name">群：{{ item.groupName }}（id：{{ item.groupId }}）</div>

            <div class="fb_right" @click="handledel($event, item, 1)"><icon name="delete-o" color="red" /></div>

          </div>

          <div class="fb_main">

            <div class="fm_silder">

              <slider v-model="item.value" size="medium" @change="handleUpdate(item, 1)">
                <template #button>

                  <div class="custom-button">{{ item.value }}</div>

                </template>
              </slider>

            </div>

            <div class="fm_right">

              <van-switch v-model="item.isOpen" size="medium" @click="switchChange($event)" @change="handleUpdate(item, 1)" />

            </div>

            <div class="fm_right_down" v-if="item.llds && item.llds.length > 0" @click="downorup(item.id, $event)">

              <icon :name="item.isExpand ? 'arrow-up' : 'arrow-down'" />

            </div>

          </div>

          <div>

            <div class="father_box_child" v-for="it in item.llds" :class="{ show_card: item.isExpand, hide_card: !item.isExpand }">

              <div class="fb_top">

                <div class="fb_name">{{ it.userName }}（id：{{ it.userId }}）</div>

                <div class="fb_right" @click="handledel($event, item, 2)"><icon name="delete-o" color="red" /></div>

              </div>

              <div class="fb_main">

                <div class="fm_silder">

                  <slider v-model="it.value" size="medium" @change="handleUpdate(it, 2)">
                    <template #button>

                      <div class="custom-button">{{ it.value }}</div>

                    </template>
                  </slider>

                </div>

                <div class="fm_right">

                  <van-switch v-model="it.isOpen" size="medium" @click="switchChange($event)" @change="handleUpdate(it, 2)" />

                </div>

              </div>

            </div>

          </div>

        </div>

      </div>

      <!-- 标签2 -->

      <div class="my_box" v-if="action === 1">

        <div class="father_box" v-for="item in listByN" :key="item.id">

          <div class="fb_top">

            <div class="fb_name">{{ item.groupName }}（id：{{ item.groupId }}）</div>

            <div class="fb_right" @click="handledel($event, item, 1)"><icon name="delete-o" color="red" /></div>

          </div>

          <div class="fb_main fb_main_spec">

            <!-- <div class="fm_silder">
              <slider v-model="item.value" size="medium" @change="handleUpdate(item, 1)"
                ><template #button>
                  <div class="custom-button">{{ item.value }}</div>
                </template></slider
              >
            </div> -->

            <!-- <div class="fm_right">
              <van-switch v-model="item.isOpen" size="medium" @click="switchChange($event)" @change="handleUpdate(item, 1)" />
            </div> -->

            <div class="fm_right_down" v-if="item.nnds && item.nnds.length > 0" @click="downorup(item.id, $event)">

              <icon :name="item.isExpand ? 'arrow-up' : 'arrow-down'" />

            </div>

          </div>

          <div>

            <div class="father_box_child" v-for="it in item.nnds" :class="{ show_card: item.isExpand, hide_card: !item.isExpand }">

              <div class="fb_top">

                <div class="fb_name">{{ it.userName }}（id：{{ it.userId }}）</div>

              </div>

              <div class="fb_main">

                <div class="fm_silder">

                  <van-field class="other_box_shadow" v-model="it.value" placeholder="请输入值" @change="handleUpdate(it, 2)"></van-field>

                </div>

                <div class="fm_right">

                  <van-switch v-model="it.isOpen" size="medium" @click="switchChange($event)" @change="handleUpdate(it, 2)" />

                </div>

              </div>

            </div>

          </div>

        </div>

      </div>

    </div>

    <van-tabbar v-model="action">

      <van-tabbar-item icon="credit-pay" @click="action_one">类型H</van-tabbar-item>

      <van-tabbar-item icon="debit-pay" @click="action_Two">类型N</van-tabbar-item>

    </van-tabbar>

  </section>

</template>

<script type="text/babel">
import Header from '@/component_basics/Header'
import { Button, Collapse, Icon, Slider, Switch as VanSwitch, Dialog, Tabbar, TabbarItem, Field, Toast } from 'vant'
import { kzQuery, kzUpdate, kzDel, kzQueryN, kzUpdateN, kzDelN } from '@/services'
export default {
  name: 'index',
  components: {
    Header,
    Icon,
    Button,
    Slider,
    VanSwitch,
    Collapse,
    [Dialog.Component.name]: Dialog.Component,
    'van-tabbar': Tabbar,
    'van-tabbar-item': TabbarItem,
    'van-field': Field
  },
  data() {
    return {
      title: '首页',
      action: 0,
      list: [],
      listByN: []
    }
  },
  mounted() {
    const query = this.$route.query
    if (query.action == 0 || query.action == 1) {
      this.action = query.action * 1
    } else {
      this.action = 0
    }
    this.getData()
  },
  methods: {
    getData() {
      if (this.action === 0) {
        kzQuery({ page: 1 }).then(res => {
          if (res && res.flag === 1) {
            this.list = res.data.list.map(item => {
              return {
                ...item,
                isExpand: false,
                isOpen: !!item.status,
                llds: item.llds.map(it => {
                  return {
                    ...it,
                    isOpen: !!it.status
                  }
                })
              }
            })
          }
        })
      } else {
        kzQueryN({ page: 1 }).then(res => {
          if (res && res.flag === 1) {
            this.listByN = res.data.list.map(item => {
              return {
                ...item,
                isExpand: false,
                isOpen: !!item.status,
                nnds: item.nnds.map(it => {
                  return {
                    ...it,
                    isOpen: !!it.status,
                    value: it.value > 100 ? (it.value / 100).toFixed(2) : it.value
                  }
                })
              }
            })
          }
        })
      }
    },
    handleUpdate(info, type) {
      if (this.action === 0) {
        kzUpdate({ type, id: info.id, groupId: info.groupId, userId: info.userId, value: info.value, status: info.isOpen }).then(res => {
          if (res && res.flag === 1) {
            Toast.success('更新成功')
          }
        })
      } else {
        kzUpdateN({
          type,
          id: info.id,
          groupId: info.groupId,
          userId: info.userId,
          value: info.value.includes('.') ? info.value * 100 : info.value * 1,
          status: info.isOpen
        }).then(res => {
          if (res && res.flag === 1) {
            Toast.success('更新成功')
          }
        })
      }
    },
    // 删除折叠框父级事件；
    handledel(event, info, type) {
      event.stopPropagation()
      Dialog.confirm({
        title: '标题',
        message: '确定删除吗？'
      })
        .then(() => {
          if (this.action === 0) {
            kzDel({ type, id: info.id, groupId: info.groupId, userId: info.userId }).then(res => {
              if (res && res.flag === 1) {
                Toast.success('删除成功')
                this.getData()
              }
            })
          } else {
            kzDelN({ type, id: info.id, groupId: info.groupId, userId: info.userId }).then(res => {
              if (res && res.flag === 1) {
                Toast.success('删除成功')
                this.getData()
              }
            })
          }
        })
        .catch(() => {})
    },
    switchChange(event) {
      event.stopPropagation()
    },
    //展开收起折叠框事件
    downorup(id, event) {
      event.stopPropagation()
      if (this.action === 0) {
        const index = this.list.findIndex(item => item.id == id)
        this.list[index].isExpand = !this.list[index].isExpand
      } else {
        const index = this.listByN.findIndex(item => item.id == id)
        this.listByN[index].isExpand = !this.listByN[index].isExpand
      }
    },
    //Tabbar切换
    action_one() {
      this.action = 0
      this.getData()
    },
    action_Two() {
      this.action = 1
      this.getData()
    },
    inputNum() {
      event.stopPropagation()
    },
    //跳转详情页面
    toedit(item) {
      this.$router.push({
        path: `/indexedit`,
        query: { ...item, action: this.action }
      })
    }
  }
}
</script>

<style lang="less" rel="stylesheet/less">
.my_index {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: #fff;
  padding: 0.3rem;

  .pt-20 {
    padding-top: 1.17333rem;
    padding-bottom: 1.8rem;
  }

  .index_wrap {
    .addbtn {
      text-align: right;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    .father_box {
      border: 1px solid #ccc;
      background-color: #fff;
      border-radius: 0.12rem;
      display: flex;
      flex-direction: column;
      margin-bottom: 0.2rem;
    }
    .father_box_child {
      background-color: #fff;
      border-radius: 0.12rem;
      display: flex;
      flex-direction: column;
      margin-bottom: 0.2rem;
      padding-left: 0.5rem;
      transition: all 0.3s;
    }
    .show_card {
      height: auto;
      overflow: visible;
    }
    .hide_card {
      height: 0;
      overflow: hidden;
    }
    .my_box {
      margin-top: 0.3rem;
      .fb_top {
        padding: 0.3rem 0.6rem;
        display: flex;
        background-color: #fff;
        border-radius: 0.12rem;
        justify-content: space-between;
        .fb_name {
          flex: 1;
          text-align: left;
        }
        .fb_right {
          width: 1rem;
        }
      }

      .fb_main {
        display: flex;
        align-items: center;
        padding: 0.3rem 0.6rem 0.3rem 0;
        .fm_silder {
          flex: 1;
          text-align: center;
          padding: 0 0.6rem;
          .other_box_shadow {
            width: 4rem;
            box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
            padding: 0.15rem 0.3rem;
          }
        }
        .fm_sildertwo {
          flex: 1;
          text-align: center;
          padding: 0 0.2rem;
        }
        .fm_right {
          width: 2.5rem;
        }
        .fm_right_down {
          width: 1rem;
        }
      }
      .fb_main_spec {
        display: flex;
        justify-content: flex-end;
        padding: 0;
        padding-right: 0.6rem;
      }
    }
  }
  .custom-button {
    width: 26px;
    color: #fff;
    font-size: 10px;
    line-height: 18px;
    text-align: center;
    background-color: #489c2f;
    border-radius: 100px;
  }
}
</style>

